package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.address.domain.vo.ChinaCascadeVo;
import com.ruoyi.common.annotation.EasyCache;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.domain.Chinas;
import com.ruoyi.system.mapper.ChinasMapper;
import com.ruoyi.system.service.IChinasService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 中国省市区Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-24
 */
@Service
public class ChinasServiceImpl implements IChinasService {

    private static final String country_redis_key = "country:province";

    @Autowired
    private ChinasMapper chinasMapper;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private IChinasService chinasService;

    /**
     * 类加载结束将地区信息添加到redis中
     **/
//    @PostConstruct
    public void initCache() {
        clearCache();
        initAreaToCache();
    }

    /**
     * 清除缓存
     **/
    private void clearCache() {
        redisCache.deleteObject(country_redis_key);
    }

    /**
     * 初始化area信息到缓存中
     *
     *
     * @date 2022/4/24 9:48
     * @return void
     **/
    public synchronized List<Chinas> initAreaToCache() {
        System.out.println("--------实现缓存地区代码--------");
        List<Chinas> cacheList = redisCache.getCacheList(country_redis_key);
        if (CollUtil.isNotEmpty(cacheList)) {
            return cacheList;
        }
        List<Chinas> chinas = chinasMapper.selectChinasList(new Chinas());
        redisCache.setCacheList(country_redis_key, chinas);
        return chinas;
    }


    @Override
    public List<Chinas> getRegions() {
        // 全部省数据
        List<Chinas> cacheList = redisCache.getCacheList(country_redis_key);
        if (CollUtil.isEmpty(cacheList)) {
            cacheList = initAreaToCache();
        }
        List<Chinas> chinas = cacheList.stream()
                .filter(china -> "0".equals(china.getPid() + "") && "1".equals(china.getLevel()))
                .collect(Collectors.toList());

        List<Long> pid = new ArrayList<>();
        chinas.forEach(item -> {
            pid.add(item.getCode());
        });
        //全部市数据
        List<Chinas> json1 = cacheList.stream()
                .filter(china -> pid.contains(china.getPid()) && "2".equals(china.getLevel()))
                .collect(Collectors.toList());
        Map<Long, List<Chinas>> cdv = json1.stream().collect(Collectors.groupingBy(Chinas::getPid));
        chinas.stream().forEach(a -> a.setCityList(cdv.get(a.getCode())));

        List<Long> pid1 = new ArrayList<>();
        json1.forEach(item -> {
            pid1.add(item.getCode());
        });
        //全部县 区数据
        List<Chinas> json2 = cacheList.stream()
                .filter(china -> pid1.contains(china.getPid()) && "3".equals(china.getLevel()))
                .collect(Collectors.toList());
        Map<Long, List<Chinas>> cdv2 = json2.stream().collect(Collectors.groupingBy(Chinas::getPid));
        chinas.stream().filter(a -> a.getCityList() != null).forEach(a -> a.getCityList().stream()
                .forEach(i -> i.setAreaList(cdv2.get(i.getCode()))));

        return chinas;
    }

    @Override
    public List<ChinaCascadeVo> getRegionsForCascade() {
        List<Chinas> regions = chinasService.getRegions();
        List<ChinaCascadeVo> chinaVos = new ArrayList<>();
        for (Chinas region : regions) {
            ChinaCascadeVo chinaVo = new ChinaCascadeVo();
            chinaVo.setLabel(region.getName());
            chinaVo.setValue(region.getCode().toString());
            List<ChinaCascadeVo> children1 = new ArrayList<>();
            for (Chinas chinas : region.getCityList()) {
                ChinaCascadeVo chinaVo2 = new ChinaCascadeVo();
                chinaVo2.setLabel(chinas.getName());
                chinaVo2.setValue(chinas.getCode().toString());
                List<Chinas> areaList = chinas.getAreaList();
                List<ChinaCascadeVo> chinaVos1 = new ArrayList<>();
                for (Chinas chinas1 : areaList) {
                    ChinaCascadeVo chinaVo3 = new ChinaCascadeVo();
                    chinaVo3.setLabel(chinas1.getName());
                    chinaVo3.setValue(chinas1.getCode().toString());
                    chinaVos1.add(chinaVo3);
                }
                chinaVo2.setChildren(chinaVos1);
                children1.add(chinaVo2);
            }
            chinaVo.setChildren(children1);
            chinaVos.add(chinaVo);
        }
        return chinaVos;
    }

    @Override
    public List<Chinas> getChinasFromCache() {
        List<Chinas> cacheList = redisCache.getCacheList(country_redis_key);
        if (CollUtil.isEmpty(cacheList)) {
            cacheList = initAreaToCache();
        }
        return cacheList;
    }
}
